// kpi指标录入   这个文件同时被ProjectReportEditor和IterationReportEditor这两个js文件引用
Ext.define('App.report.view.KpisGridField', {
    extend: 'Extend.form.field.GridField',
    alias: ['widget.KpiGridField'],
    submitFields: ['id', 'name', 'field', 'value'],
    requires: ['App.report.model.KpiModel'],
    tbar: {add: {hidden: true}, remove: {hidden: true}}, border: false,
    model: 'App.report.model.KpiModel',
    columns: [{
        header: '指标名称',
        dataIndex: 'field',
        renderer: function (v, m, r) {
            if (r.get('automaticCalculation') != true) {
                return v;
            } else {
                return Ext.String.format('<span style="color: gray;">{0}</span>', v);
            }
        }
    }, {
        header: '值',
        dataIndex: 'value',
        editor: {xtype: 'textfield'},
        renderer: function (v, m, r) {
            if (r.get('automaticCalculation') != true) {
                return v;
            } else {
                return Ext.String.format('<span style="color: gray;">{0}</span>', v);
            }
        }
    }],
    listeners: {
        beforeedit: function (editor, context, eOpts) {
            return context.record.get('automaticCalculation') != true;
        },
        startedit: function (rowedit, e, eOpts) {
            var record = e.record;
            var columns = rowedit.grid.columns;
            var inputValueEditor = this.createInputValueEditor(record, columns[1])
            inputValueEditor.setValue(record.get('value'));
            inputValueEditor.setReadOnly(false);
        }
    },
    createInputValueEditor: function (field, column) {
        var inputValueEditor = column.getEditor();
        var dataType = field.data.dataType;
        if (dataType == 'string') {
            if (!(inputValueEditor.xtypesMap.textfield == true && inputValueEditor.xtypesMap.numberfield != true)) {
                inputValueEditor = Ext.create({
                    xtype: 'textfield',
                    width: inputValueEditor.getWidth()
                });
                column.setEditor(inputValueEditor);
            }
        } else if (dataType == 'int' || dataType == 'float') {
            if (!(inputValueEditor instanceof Ext.form.field.Number)) {
                inputValueEditor = Ext.create({
                    xtype: 'numberfield',
                    width: inputValueEditor.getWidth()
                });
                column.setEditor(inputValueEditor);
            }
            inputValueEditor.decimalPrecision = (dataType == 'int' ? 0 : 4);
        } else if (dataType == 'date') {
            if (!(inputValueEditor instanceof Ext.form.field.Date)) {
                inputValueEditor = Ext.create({
                    xtype: 'datefield',
                    format: 'Y-m-d',
                    editable: false,
                    width: inputValueEditor.getWidth()
                });
                column.setEditor(inputValueEditor);
            }
        }
        return inputValueEditor;
    }
});